package com.ztbsuper.dingding;


import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;

/**
 * 钉钉通知
 * @author rubekid
 *
 * 2017年6月27日 下午11:39:14
 */
public class DingdingNotifier extends Notifier {
	
	/**
	 * access token
	 */
    private String accessToken;
    
    /**
     * jenkins地址
     */
    private String jenkinsURL;
    
    /**
     * 跳转地址
     */
    private String redirectURL;
    
    /**
     * 图标
     */
    private String icon;
    
    /**
     * 模板
     */
    private String template;

    private boolean onStart;

    private boolean onSuccess;

    private boolean onFailed;
    
    /**
     * 使用卡片发送
     */
    private boolean useActionCard;
    
    public String getAccessToken() {
        return accessToken;
    }
    
    public String getJenkinsURL() {
		return jenkinsURL;
	}
    
    public String getRedirectURL() {
        return redirectURL;
    }

    public String getIcon() {
		return icon;
	}
    
    public String getTemplate() {
		return template;
	}

	public boolean isOnStart() {
        return onStart;
    }

    public boolean isOnSuccess() {
        return onSuccess;
    }

    public boolean isOnFailed() {
        return onFailed;
    }

    public boolean isUseActionCard() {
		return useActionCard;
	}

	@DataBoundConstructor
    public DingdingNotifier(String accessToken, String jenkinsURL, String redirectURL, String icon, String template,
    		boolean onStart, boolean onSuccess, boolean onFailed, boolean useActionCard) {
        super();
        this.accessToken = accessToken;
        this.jenkinsURL = jenkinsURL;
        this.redirectURL = redirectURL;
        this.icon = icon;
        this.template = template;
        this.onStart = onStart;
        this.onSuccess = onSuccess;
        this.onFailed = onFailed;
        this.useActionCard = useActionCard;
    }

    public DingdingService newDingdingService(AbstractBuild build, TaskListener listener) {
    	JenkinsConfig config = new JenkinsConfig();
    	config.setAccessToken(accessToken);
    	config.setJenkinsURL(jenkinsURL);
    	config.setRedirectURL(redirectURL);
    	config.setIcon(icon);
    	config.setTemplate(template);
    	config.setOnStart(onStart);
    	config.setOnSuccess(onSuccess);
    	config.setOnFailed(onFailed);
    	config.setUseActionCard(useActionCard);
        return new DingdingServiceImpl(config, listener, build);
    }

    @Override
    public BuildStepMonitor getRequiredMonitorService() {
        return BuildStepMonitor.NONE;
    }

    @Override
    public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
        return true;
    }


    @Override
    public DingdingNotifierDescriptor getDescriptor() {
        return (DingdingNotifierDescriptor) super.getDescriptor();
    }

    @Extension
    public static class DingdingNotifierDescriptor extends BuildStepDescriptor<Publisher> {


        @Override
        public boolean isApplicable(Class<? extends AbstractProject> jobType) {
            return true;
        }

        @Override
        public String getDisplayName() {
            return "钉钉通知器配置";
        }

    }
}
